package com.zhoug.zmui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.zhoug.common.utils.AppUtils;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.zmui.R;
import com.zhoug.zmui.utils.ZMUIUtils;
import com.zhoug.zmui.utils.DialogAttributeParser;

import androidx.annotation.NonNull;

/**
 * 按钮dialog
 * 定义全局样式:
 * <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
 * <item name="zmui_style_ZMUIButtonDialog">@style/style_buttonDialog</item>
 * </style>
 * <style name="style_buttonDialog" parent="zmui_style_ZMUIButtonDialog">
 * <item name="zmui_corners">50dp</item>
 * <item name="zmui_fullWidth">false</item>
 * </style>
 *
 * @Author 35574
 * @Date 2020/7/20
 * @Description
 */
public class ZMUIButtonDialog extends ZMUIBaseDialog {
    private static final boolean DEBUG = false;
    private static final String TAG = ">>>ZMUIButtonDialog";
    /**
     * 按钮1
     */
    private String positiveText;
    /**
     * 按钮2
     */
    private String negativeText;
    /**
     * 按钮1监听
     */
    private View.OnClickListener mPositiveListener;
    /**
     * 按钮2监听
     */
    private View.OnClickListener mNegativeListener;

    private int positiveBtnColor ;
    private float positiveBtnSize;
    private int negativeBtnColor ;
    private float negativeBtnSize;
    // POSITIVE
    //  NEGATIVE
    //NEUTRAL

    private TextView mPositiveBtn;
    private View mLine2;
    private TextView mNegativeBtnBtn;

    public ZMUIButtonDialog(@NonNull Context context) {
        this(context,0);
    }

    public ZMUIButtonDialog(@NonNull Context context, int themeResId) {
        this(context, themeResId,R.attr.zmui_style_ZMUIButtonDialog, R.style.zmui_style_ZMUIButtonDialog);
    }

    protected ZMUIButtonDialog(@NonNull Context context, int themeResId, int defStyleAttr, int defStyleRes) {
        super(context, themeResId, defStyleAttr, defStyleRes);
    }

    @Override
    protected void initialize(@NonNull Context context, int defStyleAttr, int defStyleRes) {
        super.initialize(context, defStyleAttr, defStyleRes);
        DialogAttributeParser.parseZMUIButtonDialogAttrs(this,context,defStyleAttr, defStyleRes);
        setWidth(ZMUIUtils.getMatchParentMaxWidth());
        setGravity(Gravity.CENTER);
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.zmui_button_dialog;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        findViews();
        //是否显示分割线2
        if (mPositiveListener != null && mNegativeListener != null && mLine2 != null) {
            mLine2.setVisibility(View.VISIBLE);
        } else {
            if (mLine2 != null) {
                mLine2.setVisibility(View.GONE);
            }
        }
        //设置2个按钮
        if (mPositiveBtn != null && mPositiveListener != null) {
            ZMUIUtils.setTextColor(mPositiveBtn, positiveBtnColor);
            ZMUIUtils.setTextSize(mPositiveBtn, positiveBtnSize);
            mPositiveBtn.setText(positiveText);
            mPositiveBtn.setOnClickListener(mPositiveListener);
            mPositiveBtn.setVisibility(View.VISIBLE);
        }
        if (mNegativeBtnBtn != null && mNegativeListener != null) {
            ZMUIUtils.setTextColor(mNegativeBtnBtn, negativeBtnColor);
            ZMUIUtils.setTextSize(mNegativeBtnBtn, negativeBtnSize);
            mNegativeBtnBtn.setText(negativeText);
            mNegativeBtnBtn.setOnClickListener(mNegativeListener);
            mNegativeBtnBtn.setVisibility(View.VISIBLE);
        }

        if (positiveBtnSize != negativeBtnSize) {
            //文字大小不同时,按钮的高度会不一样
            //计算按钮的最大高度
            AppUtils.measureView(mPositiveBtn);
            AppUtils.measureView(mNegativeBtnBtn);
            int maxBtnHeight = Math.max(mPositiveBtn.getMeasuredHeight(), mNegativeBtnBtn.getMeasuredHeight());
            if (DEBUG) {
                LogUtils.d(TAG, "init:maxBtnHeight=" + maxBtnHeight);
            }
            if (maxBtnHeight > 0) {
                mPositiveBtn.setHeight(maxBtnHeight);
                mNegativeBtnBtn.setHeight(maxBtnHeight);
            }
        }
    }

    private void findViews() {
        mPositiveBtn = findViewById(R.id.btn_positive);
        mLine2 = findViewById(R.id.line2);
        mNegativeBtnBtn = findViewById(R.id.btn_negative);
    }


    /**
     * 设置按钮1 监听
     *
     * @param text
     * @param listener
     */
    public ZMUIButtonDialog setPositiveListener(String text, View.OnClickListener listener) {
        this.positiveText = text;
        this.mPositiveListener = listener;
        return this;
    }

    /**
     * 设置按钮2 监听
     *
     * @param text
     * @param listener
     */
    public ZMUIButtonDialog setNegativeListener(String text, View.OnClickListener listener) {
        this.negativeText = text;
        this.mNegativeListener = listener;
        return this;
    }

    public ZMUIButtonDialog setPositiveBtnColor(int positiveBtnColor) {
        this.positiveBtnColor = positiveBtnColor;
        return this;
    }

    public ZMUIButtonDialog setPositiveBtnSize(float positiveBtnSize) {
        this.positiveBtnSize = positiveBtnSize;
        return this;
    }

    public ZMUIButtonDialog setNegativeBtnColor(int negativeBtnColor) {
        this.negativeBtnColor = negativeBtnColor;
        return this;
    }

    public ZMUIButtonDialog setNegativeBtnSize(float negativeBtnSize) {
        this.negativeBtnSize = negativeBtnSize;
        return this;
    }

}
